package artid.com.arttogo.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import artid.com.arttogo.R;
import artid.com.arttogo.bean.ArtistFollow;
import artid.com.arttogo.bean.ArtistList;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.task.CallbackSky;
import artid.com.arttogo.utils.MyBitmapImageViewTarget;
import artid.com.arttogo.view.CircleImageView;

public class ArtCollectorsAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater mInflater;
    ViewHolder holder;
    CallbackSky cs;
    List<View> listIcon;
    public List<ArtistList> listdata;
    private ImageLoader mImageLoader;
    String reqstr;

    public ArtCollectorsAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        listIcon = new ArrayList<View>();
        listdata = new ArrayList<ArtistList>();
        RequestQueue mQueue = Volley.newRequestQueue(mContext);
        mImageLoader = new ImageLoader(mQueue, new BitmapCache());
    }

    public void setdata(List<ArtistList> listdata, String reqstr) {
        this.listdata = listdata;
        this.reqstr = reqstr;
        notifyDataSetChanged();
    }

    public void addlist(List<ArtistList> listdata) {
        this.listdata.addAll(listdata);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listdata.size();
    }

    @Override
    public View getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_art_collectors, null);
            vh.textcp = (TextView) convertView.findViewById(R.id.item_artist_cp);
            vh.textDz = (TextView) convertView.findViewById(R.id.item_artist_dz);
            vh.textgz = (TextView) convertView.findViewById(R.id.item_artist_gz);
            vh.btnGz = (View) convertView.findViewById(R.id.item_artist_btn_gz);
            vh.iv = (CircleImageView) convertView.findViewById(R.id.iv_icon);
            vh.textname = (TextView) convertView.findViewById(R.id.item_artistlist_name);
            vh.authIcon = (ImageView) convertView.findViewById(R.id.item_artist_authicon);
            vh.btnqxgz = (View) convertView.findViewById(R.id.item_artlist_qxgz);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        ArtistList item = listdata.get(position);
//		ImageListener listener = ImageLoader.getImageListener(vh.iv,
//				R.drawable.ic_menu_rotate,
//				R.drawable.ic_menu_rotate);
//		mImageLoader.get(item.getImgurl()+ "_" + "100" + "x10.png", listener);
        Glide.with(mContext).load(item.getImgurl() + "_" + "100" + "x10.png").asBitmap().centerCrop()
                .placeholder(R.drawable.ic_menu_rotate).error(R.drawable.ic_menu_rotate)
                .into(new MyBitmapImageViewTarget(vh.iv));

        vh.textcp.setText(item.getArt_num());

        vh.textDz.setText(item.getCommend_num());

        vh.textgz.setText(item.getFollow_num());

        vh.textname.setText(item.getRealname());
        String isAuth = item.getIs_auth();
        if (isAuth.equals("1")) {
            vh.authIcon.setVisibility(View.VISIBLE);
        } else {
            vh.authIcon.setVisibility(View.GONE);
        }
        String isfollow = item.getIs_follow();
        vh.btnGz.setOnClickListener(listenerGz(position, item.getId(), true));

        if (isfollow.equals("1")) {
            vh.btnqxgz.setVisibility(View.VISIBLE);
            vh.btnqxgz.setOnClickListener(listenerGz(position, item.getId(), false));
            vh.btnGz.setVisibility(View.GONE);
        } else {
            vh.btnqxgz.setVisibility(View.GONE);
            vh.btnGz.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    View.OnClickListener listenerGz(final int index, final String id, final Boolean setFollow) {
        return new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (!AppUtil.LoginVer(mContext)) {
                    reqData(index, id, setFollow);
                }
            }
        };
    }

    void reqData(final int index, String id, final Boolean setFollow) {
        String state = "";
        if (setFollow) {
            state = "1";
        } else {
            state = "2";
        }
        VolleyBasic.getInstanceNoDialog((Activity) mContext).post(Cache.artistFollow,
                VolleyMap.getInstance().artistFollow(AppUtil.getUser().getToken(), id, state), ArtistFollow.class,
                new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        ArtistFollow itemArtistFollow = (ArtistFollow) vd.getData();
                        ArtistList item = listdata.get(index);
                        if (itemArtistFollow.getState().equals("1")) {
                            if (setFollow) {
                                item.setIs_follow("1");
                            } else {
                                item.setIs_follow("2");
                            }
                            listdata.set(index, item);
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    class ViewHolder {
        TextView textgz, textcp, textDz, textname;
        CircleImageView iv;
        ImageView authIcon;
        View btnGz, btnqxgz;

    }


}
